<div class="layui-fluid layui-anim febs-anim" id="import-excel" lay-title="导入页面">
    <div class="febs-container layui-card">
        <div class="layui-card-body">
            <div id="query-project-form">
                <form method="post" class="layui-form" lay-filter="import-detail-table-form" id="import-detail-table-form">
                    <div class="layui-form-item">
                        <label class="layui-form-label">年份</label>
                        <div class="layui-input-block">
                            <input type="text" class="layui-input" name="year" id="date-select">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">模板类型</label>
                        <div class="layui-input-block">
                            <select name="templateType" lay-verify="select-template">
<!--                                其中2，3，13在12.29合并为一个导入表-->
                                <option value="1">第三方支出&增值税进项</option>
                                <option value="2">固定资产摊销</option>
                                <option value="3">办公场地装修费摊销</option>
                                <option value="4">项目房租（管理人员）</option>
                                <option value="5">招标代理服务费</option>
                                <option value="6">社保比例</option>
                                <option value="7">员工宿舍房租费</option>
                                <option value="8">残保金分摊</option>
                                <option value="9">管理人员分摊</option>
                                <option value="10">房租补助</option>
                                <option value="11">项目年终预提标准(成本)</option>
                                <option value="12">节假日及其他福利标准(费用)</option>
                                <option value="13">办公场地场租费（座席租赁费）</option>
                                <option value="14">场地费用分摊</option>
                                <option value="15">招聘费</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">选择文件</label>
                        <div class="layui-input-block">
                            <button type="button" class="layui-btn febs-button-green-plain" id="uploadFile">
                                <i class="layui-icon">&#xe67c;</i>上传文件
                            </button>
<!--                            <button type="button" class="layui-btn febs-button-blue-plain" id="importData" style="margin-left: 10px">-->
<!--                                开始导入-->
<!--                            </button>-->
                            <button type="button" class="layui-btn febs-button-blue-plain" id="template-download" style="margin-left: 10px">
                                <i class="layui-icon">&#xe82a;</i>模板下载
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script data-th-inline="none" type="text/javascript">
    layui.use(['laydate', 'jquery', 'laydate', 'febs', 'form', 'eleTree', 'table', 'validate', 'xmSelect', 'layer', 'upload'], function () {
        let $ = layui.jquery,
            laydate = layui.laydate,
            febs = layui.febs,
            form = layui.form,
            xmSelect = layui.xmSelect,
            $view = $('#import-excel'),
            $submit = $view.find('#detail-submit'),
            $detailComment = $view.find('#detail-comment'),
            $query_detail = $view.find('#query-detail'),
            $queryDetailTableForm = $view.find('#query-detail-table-form'),
            $yearMonthDisplay = $view.find('#year_month_display'),
            // $importData = $view.find('#importData'),
            $importDetailTableForm = $view.find('#import-detail-table-form'),
            $layer = layui.layer,
            upload = layui.upload;

        laydate.render({
            elem: '#date-select',
            type: 'year'
        });

        form.render();

        upload.render({
            elem: '#uploadFile' //绑定元素
            ,url: '/detailCost/import' //上传接口
            ,auto: true
            ,field: "uploadFile"
            ,accept: "file"
            ,exts: 'xls|xlsx'
            //,bindAction: '#importData'
            ,multiple: false
            ,data: {
                year: function () {
                    return $importDetailTableForm.serializeJson().year;
                },
                templateType: function () {
                    return $importDetailTableForm.serializeJson().templateType;
                }
            }
            ,before : function (obj) {
                document.getElementById('uploadFile').setAttribute("disabled", "disabled")
                febs.alert.info('正在导入，请稍等...');
                febs.view.loadBar.start();
            }
            ,done: function(result){
                console.log(result);
                if (result.code !== 200) {
                    febs.view.loadBar.error();
                    if (result.code===403)
                        febs.modal.error('导入失败', '对不起，您暂无该操作权限');
                    else
                        febs.modal.error('导入失败', result.message);
                } else {
                    febs.view.loadBar.finish();
                    febs.alert.success(result.message);
                }
                document.getElementById('uploadFile').removeAttribute("disabled")
            }
            ,error: function(){
                febs.alert.error('导入数据出错了');
                document.getElementById('uploadFile').removeAttribute("disabled")
            }
        });

        // $importData.on('click', function () {
        //     let isUpload = document.getElementsByClassName("layui-upload-choose");
        //     if (isUpload.length === 0) {
        //         febs.alert.error('请选择文件');
        //     }
        // });

        $view.on('click', '#template-download', function () {
            let nameIndex = ['', '第三方支出', '固定资产分摊', '装修费分摊', '项目房租(管理人员)', '招标代理服务费',
                '社保比例', '员工宿舍房租费', '残保金分摊', '管理人员分摊', '房租补助', '项目年终预提标准', '节假日及其他福利标准',
            '办公场地场租费（座席租赁费）', '场地费用分摊', '招聘费']
            let type = $importDetailTableForm.serializeJson().templateType;
            let url = '/febs/excelTemplate/' + type + '.xlsx'
            let fileName = '导入模板-' + nameIndex[type] + '.xlsx'
            febs.download(url, null, fileName);
        });
    });
</script>